<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Device;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Category;
use App\Models\Manufacturer;
use App\Models\Place;
use App\Models\User;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Widgets\Card;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use App\Models\Part;
use Dcat\Admin\Admin;
use App\Admin\Renderable\PartTable;
use App\Admin\Renderable\RecordTable;
use App\Admin\Actions\Grid\ChangeUser;
use Illuminate\Support\Str;
use App\Admin\Actions\Grid\ResetUser;
use Illuminate\Http\Request; 
use App\Admin\Extensions\Tools\PrintButton;













class DeviceController extends AdminController
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new \App\Admin\Repositories\Device();
    }
   
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
      
            
        return Grid::make(new Device(), function (Grid $grid) {

            $grid->tableCollapse(false);
            $userId = Admin::user()->id;
            if ($userId == 1) {
                // 当 $userId 等于 1 时不执行 where 条件
            } else {
                $grid->model()->where('User', $userId);
            }
            $grid->column('id')->sortable();
            $grid->column('Asset_Number')->label('orange');
           
            $grid->column('Name');
            $grid->column('Category_id')->display(function ($category) {
                return Category::find($category)->category;
            });
            $grid->column('Manufacturer_id')->display(function ($manufacturer) {
                return Manufacturer::find($manufacturer)->manufacturer;
            });
            $grid->column('MAC');
            $grid->column('IP');
            $grid->column('Model');
            $grid->column('User_id')->label('input-border');

            $grid->column('Location_id')->display(function ($Place) {
                return Place::find($Place)->place;
            });

            //$grid->column('record')->limit(20);
            
         
            $grid->column('Record')->display('历史信息')->modal(function (Grid\Displayers\Modal $modal) {
                // 标题
                $modal->title('历史信息');
                // 自定义图标
                $modal->icon('feather icon-edit');
                // 传递当前行字段值
                return RecordTable::make()->payload(['Record' => $this->record]);});


            $grid->column('State')->display(function ($state) {
                if ($state === '正常') {
                    return '<span style="background-color: green; color: white;">' . $state . '</span>';
                }
                if ($state === '空闲') {
                    return '<span style="background-color: black; color: white;">' . $state . '</span>';
                }
                if ($state === '报废') {
                    return '<span style="background-color: red; color: white;">' . $state . '</span>';
                }
                return $state;
            });
            $grid->column('Remark')->textarea();
            $grid->二维码->qrcode(function () {
                return config('app.url') . '/api/devices?asset_number=' . $this->Asset_Number;
            }, 200, 200);
            $grid->column('详细信息')
                ->display('详细信息')
                ->expand(PartTable::make(['Asset_Number']));
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
        
                
                $filter->like('Name')->width(2);
                $filter->like('Model')->width(2);
                $filter->equal('Location_id')->select(Place::pluck('place', 'id'))->width(2);
                $filter->equal('Manufacturer_id')->select(Manufacturer::pluck('manufacturer', 'id'))->width(2);
                $filter->equal('Category_id')->select(Category::pluck('category', 'id'))->width(2);
                $filter->equal('User_id')->select(Administrator::pluck('name', 'id'))->width(3);
                $filter->like('State')->width(3);
                
               

        
            });
            //开启弹窗创建表单
            $grid->enableDialogCreate();
            // 显示快捷编辑按钮
            $grid->showQuickEditButton();
            // 禁用编辑按钮
            $grid->disableEditButton(); 
            //设置列选择器
            $grid->showColumnSelector();
            $grid->hideColumns(['MAC','IP','Model','Record','Remark','created_at','updated_at','id',]);

           
            // $grid->actions(function (Grid\Displayers\Actions $actions) {
            //     // 当前行的某个字段的数据
            //     //$User = $actions->row->User;
            //     $actions->append(new ResetUser());
            // });
            $grid->actions([new ResetUser()]);
            
 

            $grid->batchActions([
                new PrintButton('打印标签')
                ]);   
           
           
           
       
         
            
            

         
            
        
        
        
            
       

           
            


        });
    }

    public function printLabels(Request $request)
    {
        $ids = explode(',', $request->ids);
        $devices = \App\Models\Device::whereIn('id', $ids)->get();
        
        return view('admin.tools.label', compact('devices'));
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Device(), function (Show $show) {
          
            $show->field('id');
            $show->field('Asset_Number');
            $show->field('Name');
            $show->field('Category_id');
            $show->field('Manufacturer_id');
            $show->field('MAC');
            $show->field('IP');
            $show->field('Model');
            $show->field('User_id');
            $show->field('Location_id');
            $show->field('Record');
            $show->field('State');
            $show->field('Remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Device(), function (Form $form) {
            $form->display('id');
          

            $hour = 10;
            $hourFormatted = sprintf('%02d', $hour);
            $assetNumber = 'FMS-' . date('Ymd') . '-' . $hourFormatted . date('is');
            $form->text('Asset_Number')->default($assetNumber)->required();
            $form->text('Name')->required();
            $form->select('Category_id', '分类')->required()->options(function () {
                return Category::pluck('category', 'id');
            });
            $form->select('Manufacturer_id', '厂商')->required()->options(function () {
                return Manufacturer::pluck('Manufacturer', 'id');
            });
            
            //$form->text('MAC');
            //$form->text('IP');
            $form->text('Model');

           
          
         
            $form->select('User_id', '用户')->options(function () {
                return Administrator::pluck('name', 'name');
            });
            

            $form->select('Location_id', '位置')->required()->options(function () {
                return Place::pluck('Place', 'id');
            });
           
            //$form->textarea('Record')->rows(5)->help('每一条记录都要以&lt;br&gt;结尾');
            $form->hidden('State')->default('正常');

            $form->text('Remark');
        
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                if (empty($form->User_id)) {
                    $form->State = '空闲';
                } else {
                    $form->State = '正常';
                }
            });
        });
        
    }
}
